package org.bagou.xiangs.framework_mvvm.older

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.databinding.library.baseAdapters.BR
import androidx.recyclerview.widget.RecyclerView
import org.bagou.xiangs.framework_mvvm.R
import org.bagou.xiangs.framework_mvvm.databinding.ItemUserOldDatabindingBinding

class DataBindingOldAdapter : RecyclerView.Adapter<DataBindingOldAdapter.MvvmViewHolder>() {

    private var dataList: ArrayList<WechatItem> = ArrayList()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MvvmViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding: ItemUserOldDatabindingBinding =
            DataBindingUtil.inflate<ItemUserOldDatabindingBinding>(
                layoutInflater,
                R.layout.item_user_old_databinding,
                parent,
                false
            )
        binding.root.setBackgroundColor(Color.WHITE)
        return MvvmViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: MvvmViewHolder, position: Int) {
        onViewLayoutSet(holder, position)
        holder.binding.setVariable(BR.infoItem, dataList[position])

    }


    fun addData(data: List<WechatItem>) {
        dataList.addAll(data)
        notifyDataSetChanged()
    }

    inner class MvvmViewHolder(val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root)

    // 设置控件样式
    private fun onViewLayoutSet(holder: MvvmViewHolder, position: Int) {
        val layoutParams: ViewGroup.LayoutParams = holder.binding.root.layoutParams
        when (position) {
            0 -> {
                (layoutParams as? ViewGroup.MarginLayoutParams)?.topMargin = 30
                (layoutParams as? ViewGroup.MarginLayoutParams)?.bottomMargin = 30
            }
            itemCount - 1 -> (layoutParams as? ViewGroup.MarginLayoutParams)?.topMargin = 30
            else -> (layoutParams as? ViewGroup.MarginLayoutParams)?.topMargin = 1
        }
        holder.binding.root.layoutParams = layoutParams
    }
}


